<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增模具"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="设备编码" required>
            <el-input
              v-model="state.form.equipCode"
              placeholder="请填写编码"
              @click="toSelectInv"
              @focus="toSelectInv"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称">
            <el-input v-model="state.form.equipName" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备规格">
            <el-input v-model="state.form.equipSpec" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商">
            <el-input v-model="state.form.vender" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心" required>
            <el-select
              v-model="state.form.pkWc"
              placeholder="工作中心"
              style="width: 100%"
            >
              <el-option
                v-for="wc in wcs"
                :key="wc.pkWc"
                :label="wc.wcName"
                :value="wc.pkWc"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" required>
            <el-select
              v-model="state.form.enumValue"
              placeholder="设备类型"
              style="width: 100%"
            >
              <el-option
                v-for="eqType in eqTypes"
                :key="eqType.eqType"
                :label="eqType.eqTypeName"
                :value="eqType.eqType"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getWcListApi } from '@/api/base/wc/index'
  import { getEqTypesApi, addEquipDocApi } from '@/api/equip/equip_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const eqTypes = ref()
  const wcs = ref()

  const state = reactive({
    form: {
      pkCorp: pkCorp,
    },
  })

  const openDialog = () => {
    getEqTypes()
    getWcList()
    dialogVisible.value = true
  }

  const getWcList = async () => {
    let con = { pkCorp: pkCorp }
    const res = await getWcListApi(con)
    if (res.code === 2000) {
      wcs.value = res.data
    }
  }

  const getEqTypes = async () => {
    const res = await getEqTypesApi()
    if (res.code === 2000) {
      state.form.eqType = res.data[0].enumValue
      eqTypes.value = res.data
    }
  }

  const save = async () => {
    const load = $baseLoading()
    try {
      const { code, msg } = await addEquipDocApi(state.form)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
